package com.weds.frame.common.entity;

import com.weds.frame.common.constants.HttpReturnRnums;
import com.weds.frame.common.enums.IEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/3/31 09:07
 */
@Data
@ApiModel
@NoArgsConstructor
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态码", notes = "0是成功,其他都是失败")
    private int code = HttpReturnRnums.Success.value();

    @ApiModelProperty(value = "状态码描述信息", notes = "例如失败，参数错误")
    private String msg = HttpReturnRnums.Success.desc();

    @ApiModelProperty(value = "具体的业务数据", notes = "")
    private Object data;

    public JsonResult(int code, String msg, T data) {
        setCode(code);
        setMsg(msg);
        if (Objects.nonNull(data)) {
            JsonDataResult<T> jsonDataResult = new JsonDataResult<>();
            if (data instanceof List ||
                    data instanceof Set) {
                jsonDataResult.setRecords(data);
            } else {
                jsonDataResult.setData(data);
            }
            setData(jsonDataResult);
        }
    }

    public JsonResult(int code, String msg, T data, Object exData) {
        setCode(code);
        setMsg(msg);
        if (Objects.nonNull(data)) {
            JsonDataResult<T> jsonDataResult = new JsonDataResult<>();
            if (data instanceof List ||
                    data instanceof Set) {
                jsonDataResult.setRecords(data);
            } else {
                jsonDataResult.setData(data);
            }
            setData(jsonDataResult);
        } else if (Objects.nonNull(exData)) {
            JsonDataResult<T> jsonDataResult = new JsonDataResult<>();
            jsonDataResult.setExData(exData);
            setData(jsonDataResult);
        }
    }

    public JsonResult(IEnum e, T data) {
        setCode(e.value());
        setMsg(e.desc());
        if (Objects.nonNull(data)) {
            JsonDataResult<T> jsonDataResult = new JsonDataResult<>();
            if (data instanceof List ||
                    data instanceof Set) {
                jsonDataResult.setRecords(data);
            } else {
                jsonDataResult.setData(data);
            }
            setData(jsonDataResult);
        }
    }

    public JsonResult(IEnum data) {
        setCode(data.value());
        setMsg(data.desc());
    }

    public JsonResult(int code, String msg) {
        setCode(code);
        setMsg(msg);
    }
}